package dk.simonvogensen.itodo.server.model;

import com.google.appengine.api.datastore.Text;
import com.google.gwt.user.client.rpc.IsSerializable;
import dk.simonvogensen.itodo.shared.model.Config;
import dk.simonvogensen.itodo.shared.model.TodoItem;

import javax.jdo.annotations.*;

/**
 * @author sbv
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class PersistedTodoItem implements IsSerializable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String id;

    @Persistent
    private String email;

    @Persistent
    private Text text;

    @Persistent
    private int index;

    public PersistedTodoItem() {
    }

    public PersistedTodoItem(TodoItem todoItem) {
        id = todoItem.getId();
        email = todoItem.getEmail();
        if (todoItem.getText() != null)
            text = new Text(todoItem.getText());
        else
            text = new Text("");

        index = todoItem.getIndex();
    }

    public PersistedTodoItem(int index) {
        this.index = index;
        text = new Text("");
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Text getText() {
        return text;
    }

    public void setText(Text text) {
        this.text = text;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public TodoItem createTodoItem(Config config) {
        TodoItem item = new TodoItem();
        item.setId(getId());
        item.setIndex(getIndex());
        item.setConfig(config);
        item.setText(getText().getValue());
        return item;
    }
}